package com.ttg.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.ttg.config.Properties;
import com.ttg.contants.KeyContants;
import com.ttg.data.InitGatewayReqData;
import com.ttg.pojo.RefundGoodsReqDTO;
import com.ttg.security.SecurityKit;
import com.ttg.utils.HttpKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

//~--- classes ----------------------------------------------------------------

/**
 * Class GwRefundDemoController
 * Description 说明：退货接口测试DEMO
 * Create 2016-08-09 13:38:00
 * @author Ardy
 */
@Controller
@RequestMapping
public class GwRefundDemoController {

    /**
     * Field LOGGER
     * Description
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(GwRefundDemoController.class);

    /**
     * Method testRefundDemo
     * Description 说明：退货接口
     *
     * @return 返回值说明：
     */
    @RequestMapping("/refund")
    public ModelAndView testRefundDemo() {
        RefundGoodsReqDTO refundGoods  = InitGatewayReqData.initRefundGoods();
        ModelAndView      modelAndView = new ModelAndView();
        String            url          = "req_refund";

        modelAndView.addObject("url", url);
        modelAndView.setViewName("demo/refund_req_demo");
        modelAndView.addObject("refundGoods", refundGoods);

        return modelAndView;
    }

    /**
     * Method reqRefund
     * Description 说明：
     *
     * @param merchantId 说明：
     * @param merOrderId 说明：
     * @param currency 说明：
     * @param transAmt 说明：
     * @param oriMerOrderId 说明：
     * @param oriTransDate 说明：
     * @param bgRetUrl 说明：
     *
     * @return 返回值说明：
     */
    @RequestMapping(
        value    = "/req_refund",
        method   = RequestMethod.POST
    )
    public ModelAndView reqRefund(@RequestParam(
        value    = "merchantId",
        required = true
    ) String merchantId, @RequestParam(
        value    = "merOrderId",
        required = true
    ) String merOrderId, @RequestParam(
        value    = "currency",
        required = true
    ) String currency, @RequestParam(
        value    = "transAmt",
        required = true
    ) Integer transAmt, @RequestParam(
        value    = "oriMerOrderId",
        required = true
    ) String oriMerOrderId, @RequestParam(
        value    = "oriTransDate",
        required = true
    ) String oriTransDate, @RequestParam(value = "attach",required = false)String attach, @RequestParam(
        value    = "bgRetUrl",
        required = false
    ) String bgRetUrl) {
        RefundGoodsReqDTO refundGoods = new RefundGoodsReqDTO();

        refundGoods.setMerchantId(merchantId);
        refundGoods.setMerOrderId(merOrderId);
        refundGoods.setCurrency(currency);
        refundGoods.setTransAmt(transAmt);
        refundGoods.setOriMerOrderId(oriMerOrderId);
        refundGoods.setOriTransDate(oriTransDate);
        refundGoods.setBgRetUrl(bgRetUrl);
        refundGoods.setAttach(attach);


        refundGoods.setSignatureAlgorithm(KeyContants.SECURITY_TYPE);
        refundGoods.setSignatureInfo(
            SecurityKit.signByRSA(HttpKit.httpGwParam2SignStr(refundGoods.getAllParameters())));

        ModelAndView modelAndView = new ModelAndView();
        String       url          = Properties.URL.getString("GATEWAY_BASE_SCHEME")
                                    + Properties.URL.getString("REFUND_URL");

        modelAndView.addObject("url", url);
        modelAndView.setViewName("req/refund_demo");
        modelAndView.addObject("refundGoods", refundGoods);

        return modelAndView;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
